package com.lxn.direct;

import com.lxn.ConnectionUtil;
import com.rabbitmq.client.*;

import java.io.IOException;

//接受
public class Rev {
    //队列名字
    public static final String NAME_QUEUE_DIRECT = "queue_direct2";
    public static void main(String[] args) throws Exception {
        //连接工具获取连接对象
        Connection connection = ConnectionUtil.getConnection();
        //创建通道
        Channel channel = connection.createChannel();
        //使用默认交换机
        //创建队列
        channel.queueDeclare(NAME_QUEUE_DIRECT,true,false,false,null);
        //把队列绑定交换机
        channel.queueBind(NAME_QUEUE_DIRECT, Send.NAME_EXCHANGE_DIRECT,"delete");

        //同时只处理一个消息
        channel.basicQos(1);
        //接受消息
        DefaultConsumer consumer = new DefaultConsumer(channel) {
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                //int i=1/0;
                System.out.println("收到消息："+new String(body));
                //手动签收
                channel.basicAck(envelope.getDeliveryTag(),false);
            }
        };//autoAck:true自动签收，false手动签收
        channel.basicConsume(NAME_QUEUE_DIRECT,false, consumer);
    }
}
